package cn.lzg.client.scanner;

import java.lang.reflect.Method;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import cn.lzg.common.core.annotation.SocketCmd;
import cn.lzg.common.core.annotation.SocketModule;
@Component
public class Scanner implements BeanPostProcessor {

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		Class<?> clazz = bean.getClass();
		Class<?>[] interfaces = clazz.getInterfaces();
		if (interfaces != null && interfaces.length > 0) {
			for (Class<?> interFace : interfaces) {
				SocketModule socketmodule = interFace.getAnnotation(SocketModule.class);
				if (socketmodule == null) {
					continue;
				}
				Method[] methods = interFace.getMethods();
				for (Method method : methods) {
					SocketCmd socketcmd = method.getAnnotation(SocketCmd.class);
					if (socketcmd == null) {
						continue;
					}
					Invoker invoker1 = Invoker.valueOf(bean, method);
					if (invoker1 != null) {
						if(InvokerHoler.getInvoker(socketmodule.module(), socketcmd.cmd())==null) {
							InvokerHoler.addInvoker(socketmodule.module(), socketcmd.cmd(), invoker1);
						}else {
							System.out.println("重复命令！");
						}
					}
				}
			}
		}
		return bean;
	}

}
